package com.example.dell.library.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.dell.library.R;
import com.example.dell.library.adapter.BookNameAdapter;
import com.example.dell.library.bean.Book;
import com.example.dell.library.fragment.HomeFragment;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class SearchFragment extends Fragment {
    private EditText textEdit;
    private ImageView textDele;
    private Button btnSear;
    private String textSear;
    private List<Book> bookList = new ArrayList<>();
    private RecyclerView recycler;
    String na;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_search, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        recycler = (RecyclerView) getActivity().findViewById(R.id.recyclerView);
        initViews();
    }

    private void initViews() {
        textEdit = (EditText) getActivity().findViewById(R.id.book_rea);
        textDele = (ImageView) getActivity().findViewById(R.id.text_delete);
        btnSear = (Button) getActivity().findViewById(R.id.btn_search);
        textDele.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textEdit.setText("");
            }
        });
        textEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() == 0) {
                    textDele.setVisibility(View.GONE);
                } else {
                    textDele.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        btnSear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textSear = textEdit.getText().toString();
                BmobQuery<Book> query = new BmobQuery<>();
                query.addWhereEqualTo("name",textSear);
                query.findObjects(new FindListener<Book>() {
                    @Override
                    public void done(List<Book> list, BmobException e) {
                        if (e == null) {
                            for (int i = 0; i < list.size(); i++) {
                                BmobFile img = list.get(i).getImgFile();
                                na = list.get(i).getName();
                                String au = list.get(i).getAuthor();
                                Book book = new Book(img,na,au);
                                bookList.add(book);
                            }
                            if ("".equals(textSear)||!textSear.equals(na)) {
                                Toast.makeText(getContext(), "请输入完整内容", Toast.LENGTH_SHORT).show();
                            }
                            LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
                            recycler.setLayoutManager(layoutManager);
                            BookNameAdapter nameAdapter = new BookNameAdapter(list);
                            recycler.setAdapter(nameAdapter);
                        }
                    }
                });
            }
        });
    }
}
